<?php
if ($do == 'albRotAdd') {
   $i=0;
   foreach ($_REQUEST['alb'] as $alb) {
      if ($alb[slot]) {
         // set the status of all CDs in desired slot to old 
         mysql_query("UPDATE `album` SET `status`='3' WHERE (`slot`='".$alb[slot]."') AND (`status`='2') AND (`cddbid`!='".$alb[id]."') ");
         
         // put the incoming CDs in the slot and set them to in-rotation 
         mysql_query("UPDATE `album` SET `status`='2',`slot`='".$alb[slot]."' WHERE `cddbid`='".$alb[id]."' ");
         
         //get data from album to prepare to move files 
         $mvTrkAlbQuery = mysql_query("SELECT `category`,`title` FROM `album` WHERE `cddbid`='".$alb[id]."' ");
         $mvTrkAlb = mysql_fetch_array($mvTrkAlbQuery);
         
         // move all the files of the freshly moved CDs into new places 
         $mvTrkQuery = mysql_query ("SELECT * FROM `track` WHERE `cddbid`='".$alb[id]."' ");
         while ($mvTrk = mysql_fetch_array($mvTrkQuery)) {
            $mvTrk[status] = 3;
            $mvTrk[num] = $mvTrk[trackno];
            $mvTrk[category] = $mvTrkAlb[category];
            $mvTrk[album] = $mvTrkAlb[title];
            $newFilename =mvTrkFile ($mvTrk[filepath].$mvTrk[filename], $mvTrk, $MUSIC_DIR);
            
            // once we get the new filepath and name, we've got to insert them back into the track's DB entry
            mysql_query(" UPDATE `track` SET `filepath`='".mysql_real_escape_string($newFilename[filepath])."',`filename`='".mysql_real_escape_string($newFilename[filename])."' WHERE `id`='".$mvTrk[id]."' ");
         }
         
         $multiQuery = mysql_query("SELECT `cddbid` FROM `album` WHERE `multi_id`='".$alb[id]."' ");
         while ($multi = mysql_fetch_array($multiQuery)) {
            mysql_query("UPDATE `album` SET `status`='2',`slot`='".$alb[slot]."' WHERE `cddbid`='".$multi[cddbid]."' ");
         }
      }
      $albPrint[$i++] = $alb;
   }
   include ('./mode/labelprint.inc');
} else if ($do === 'albRetire') {
   echo ('choose old albums to retire:'.count($_REQUEST['rotationImport']));
   ?><form action="<?=$_SERVER['PHP_SELF']?>" method="post"><table><tr><th>Slot#</th><th>Artist/Album</th></tr><?php 
   $i = 0;
   foreach ($_REQUEST['rotationImport'] as $alb) {
      $newAlbQuery = mysql_query("SELECT * FROM `album` WHERE `cddbid`='".$alb."' LIMIT 1 ");
      $newAlb = mysql_fetch_array($newAlbQuery);
      if ($newAlb[slot]) $slot = ' value="'.$newAlb[slot].'" ';
      else unset($slot);
      printf("<tr><td><input type=\"text\" name=\"alb[%s][slot]\" %s size=\"3\" /></td><td>
      <input type=\"hidden\" name=\"alb[%s][id]\" value=\"%s\" /> %s - <i>%s</i></td></tr>", $i, $slot, $i, $alb, $newAlb[artist], trunc($newAlb[title], 25));
      $i++;
//      if ($newArray) $newArray .= ', ';
//      else $newArray = "\'";
//      $newArray .= $alb;
   }
   ?><tr><td></td><td>
   <input type="hidden" name="do" value="albRotAdd" />
   <input type="hidden" name="mode" value="rotationImport" />
   <input type="hidden" name="category" value="<?=$_REQUEST[category]?>" />
   <input type="submit" value="Add to Rotation" /></td></tr></table></form><?php
//   echo ('<img src="./themes/'.$theme.'/img/blank.png" onLoad="newAlbIdArray = new Array('.$newArray.'); alert(newAlbIdArray[0]);" />');
//   echo('<img src="./themes/'.$theme.'/img/blank.png" onLoad="alert(newAlbIdArray[1]);" />');
   $albOldQuery = mysql_query ("SELECT * FROM `album` WHERE (`status` = '2') AND (`multi`='' or `multi`='A') ORDER BY `add_date` ASC ");
   while ($albOld = mysql_fetch_array($albOldQuery)) {
      printf("%s, %s - \"%s\"<br />", $albOld[slot], trunc($albOld[artist], 25), trunc($albOld[title], 15) );
   }
} else {
   if ($where = $_POST['where']) {
	   
   } else {
	   $where = "WHERE (`status`<'3') AND (`review` IS NOT NULL) AND ((`multi`='') OR (`multi`='A'))";
   }
   $rotationImportQuery = mysql_query("SELECT * FROM `album` " . $where . " ORDER BY `status` ASC, `add_date` DESC");

echo('<h2>Select the albums you would like to add to rotation:</h2>');
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<fieldset><legend>Display Options</legend>
<label><input type="checkbox" checked="checked" name="description" value="true" /> require description</label>
<input type="hidden" name="mode" value="rotationImport" />
<input type="submit" value="Go Go!" />
</fieldset></form>
<form action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post">
<fieldset><legend>Options</legend>
<input type="hidden" name="mode" value="rotationImport" />
<input type="hidden" name="do" value="albRetire" />
Category:<select name="category" tabindex="16">
<option value="">[Primary Collection]</option>
<?php
$category_query = mysql_query("SELECT `id`,`category`,`default` FROM `category` ORDER BY `category`");
//$category_result = mysql_query($category_query);
while ($category_row = mysql_fetch_row($category_query)) {
?><option value="<?=$category_row[0]?>"<?php 
if ($category_row[2]){
print(' selected="selected"');
}
?>><?=$category_row[1]?></option>
<?php
}
?>
</select>
<input type="submit" value="Add to Rotation" />
</fieldset>
<table>
<tr>
<td>Artist</td><td>Title</td><td>Add Date</td><td>Slot</td><td>Status</td>
</tr>
<?php
$todayNum = date('Ymd');
$i = 0;
while($rotationImport = mysql_fetch_array($rotationImportQuery)) { 
?><tr class="highlighted">
<td><input name="rotationImport[<?=$i++?>]" value="<?=$rotationImport[cddbid]?>" type="checkbox" /><?=trunc($rotationImport[artist], 20)?></td>
<td><?=trunc($rotationImport[title], 20)?></td>
<td><?php 
echo(
   round( 
      ((time() - strtotime($rotationImport[add_date]))/604800),0)
    );
//round( ((time() - strtotime($rotationImport[add_date]))) ).' 
?> weeks old.
</td>
<td><?=$rotationImport[slot] . $rotationImport[multi] ?></td>
<td><?=$rotationImport[status]?></td>
</tr>
<?php
}
?></table></form>
<?php 
}
?>